# Maintainer: David Macek <david.macek.0@gmail.com>

_realname=aria2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.35.0
pkgrel=2
pkgdesc="A multi-protocol & multi-source, cross platform download utility (mingw-w64)"
arch=('any')
url="https://aria2.sourceforge.io/"
license=('GPL2')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gettext"
         "${MINGW_PACKAGE_PREFIX}-c-ares"
         "${MINGW_PACKAGE_PREFIX}-cppunit"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libssh2"
         "${MINGW_PACKAGE_PREFIX}-libuv"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         )
optdepends=("${MINGW_PACKAGE_PREFIX}-ruby: aria2rpc and aria2mon")
source=(${_realname}-${pkgver}.tar.gz::https://github.com/tatsuhiro-t/aria2/archive/release-${pkgver}.tar.gz)
sha256sums=('c4072deaf6b443be15f9710657aa6c53527f642bc7a3252bb81d2853161080ed')

prepare() {
  cd "${_realname}-release-${pkgver}"
  autoreconf -ifv || autoreconf -ifv
}

build() {
  [[ -d build-${MINGW_CHOST} ]] && rm -rf build-${MINGW_CHOST}
  mkdir -p build-${MINGW_CHOST}
  cd build-${MINGW_CHOST}

  # disabling gnutls and wintls so we will fall back to openssl
  ../${_realname}-release-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --with-bashcompletiondir=${MINGW_PREFIX}/share/bash-completion/completions \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-static \
    --enable-shared \
    --without-gnutls \
    --without-wintls \
    --enable-libaria2

  make
}

package() {
  cd "build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install

  cd "../${_realname}-release-${pkgver}"

  install -Dm0755 doc/xmlrpc/aria2rpc "${pkgdir}"${MINGW_PREFIX}/bin/aria2rpc
  install -Dm0755 doc/xmlrpc/aria2mon "${pkgdir}"${MINGW_PREFIX}/bin/aria2mon
  echo '@ruby -- "%~dp0aria2rpc" %*' > "${pkgdir}"${MINGW_PREFIX}/bin/aria2rpc.cmd
  echo '@ruby -- "%~dp0aria2mon" %*' > "${pkgdir}"${MINGW_PREFIX}/bin/aria2mon.cmd
  rm ${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/xmlrpc/aria2{rpc,mon}
}
